package com.zb.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyEsConfig {

    @Value("${myes.host}")
    private String hosts;

    @Bean
    public RestHighLevelClient createClient() {
        String[] strings = hosts.split(",");
        HttpHost[] httpHosts = new HttpHost[strings.length];
        for (int i = 0; i < strings.length; i++) {
            String host = strings[i];
            //host=127.0.0.1:9200
            String ip = host.split(":")[0];
            Integer port = Integer.parseInt(host.split(":")[1]);
            httpHosts[i] = new HttpHost(ip, port, "http");
        }
        return new RestHighLevelClient(RestClient.builder(httpHosts));
    }

}
